home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’97 / Sessions ’97 / Java for Macintosh Applications / JRI Native / TimeUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-27  |  4.0 KB  |  211 lines  |  [TEXT/CWIE]

  1. /*
  2.     Test.java
  3.  
  4.     Test of JRI Native Methods.
  5.  
  6.     © 1997 by Michael J. Webb (mjw@codewell.com)
  7.  
  8. */
  9.  
  10. /* Mac Includes */
  11.  
  12. #include <lowmem.h>
  13. #include <OSUtils.h>
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. /* Java Runtime Headers */
  19. #include <NativeLibSupport.h>
  20. #include <jri.h>
  21.  
  22. /* Java Class Name constant - change this for your methods. */
  23. #define kJRIClassName "TimeUtils"
  24.  
  25. /* Forward declarations for our local native method implementation. */
  26.  
  27. static long GetRealTime(JRIEnv* runtimeEnv, jref this);
  28. static long GetRelativeTime(JRIEnv* runtimeEnv, jref this);
  29. static JRIStringID GetTimeString(JRIEnv* runtimeEnv, jref this, jint someTime);
  30.  
  31. /*
  32.     Signature Summary.  Remember these map to Java types, so (for example)
  33.     'Char' means a unicode character, 'Int' means long.
  34.  
  35.     Single character codes:
  36.  
  37.     Boolean            Z
  38.     Byte            B
  39.     Char            C
  40.     Double            D
  41.     Float            F
  42.     Int                I
  43.     Long            J
  44.     Short            S
  45.     Void            V (Only as a return type)
  46.  
  47.     Special codes:
  48.  
  49.     Array            [ followed by one of the other types
  50.     Object            L followed by the package of the class terminated by ';'
  51.  
  52.     The method name array is a NULL terminated array of
  53.     methodname(signature)returntype strings.
  54.  
  55.     Some examples:
  56.  
  57.     void foo();
  58.     "foo()V"
  59.  
  60.     int foo();
  61.     "foo()I"
  62.  
  63.     String foo();
  64.     "foo()Ljava/lang/String;"
  65.  
  66.     void foo(String bar);
  67.     "foo(Ljava/lang/String;)V"
  68.  
  69.     void foo(byte[]);
  70.     "foo([B)V"
  71.  
  72.     int foo(int a, int b, int c);
  73.     "foo(III)I"
  74.  
  75.     void foo(String[] a, int[][] b)
  76.     "foo(Ljava/lang/String;[[I)V"
  77.  
  78. */
  79.  
  80. /* Array of Native Method signatures. */
  81. static char* methodNames[] =
  82. {
  83.     "GetRealTime()I",
  84.     "GetRelativeTime()I",
  85.     "GetTimeString(I)Ljava/lang/String;",
  86.     NULL                                        /* NULL terminates the array. */
  87. };
  88.  
  89. /*
  90.     The procedure array is a NULL terminated array of procedure
  91.     pointers in the same order as the method signature array.
  92. */
  93.  
  94. /* Array of Native Method implementations. */
  95. static void* methodProcedures[] =
  96. {
  97.     GetRealTime,
  98.     GetRelativeTime,
  99.     GetTimeString,
  100.     NULL                                        /* NULL terminates the array. */
  101. };
  102.  
  103. /* A native method to get the current time. */
  104. long GetRealTime(JRIEnv* runtimeEnv, jref this)
  105. {
  106.     long realTime = 0;
  107.  
  108.     runtimeEnv = runtimeEnv;
  109.     this = this;
  110.  
  111.     GetDateTime(((unsigned long*)(&realTime)));
  112.  
  113.     return realTime;
  114. }
  115.  
  116. /* A native method to get the tick count. */
  117. long GetRelativeTime(JRIEnv* runtimeEnv, jref this)
  118. {
  119.     SInt32 relTime = 0;
  120.  
  121.     runtimeEnv = runtimeEnv;
  122.     this = this;
  123.  
  124.     relTime = LMGetTicks();
  125.  
  126.     return relTime;
  127. }
  128.  
  129. /* A native method to convert an integer to a time string. */
  130. static JRIStringID GetTimeString(JRIEnv* runtimeEnv, jref this, jint someTime)
  131. {
  132.     JRIStringID        newString;
  133.     DateTimeRec        dateTime;
  134.     char            buffer[256];
  135.  
  136.     SecondsToDate(someTime, &dateTime);
  137.  
  138.     sprintf
  139.     (
  140.         buffer,
  141.         "%d/%d/%d %d:%d:%d",
  142.         dateTime.day,
  143.         dateTime.month,
  144.         dateTime.year,
  145.         dateTime.hour,
  146.         dateTime.minute,
  147.         dateTime.second
  148.     );
  149.  
  150.     newString = (runtimeEnv->interface)->NewStringUTF(runtimeEnv, buffer, strlen(buffer));
  151.  
  152.     return newString;
  153. }
  154.  
  155. /*
  156.     Initialization routine for these native methods.  It will be
  157.     called at load library time.
  158.  
  159.     This implementation is pretty generic; if you change the #define of
  160.     'kJRIClassName' above for you native methods, this routine should
  161.     work.
  162. */
  163. OSStatus
  164. InitJavaLibrary
  165. (
  166.     JRIRuntimeInstance*    runtimeInstance,
  167.     CFragConnectionID    javaFragment
  168. )
  169. {
  170.     OSStatus            initResult = noErr;
  171.     JRIEnv*                runtimeEnv = NULL;
  172.     JRIClassID            classID = NULL;
  173.  
  174.     /* Try to initialize the support library. */
  175.     initResult = InitNativeLibSupport(runtimeInstance, javaFragment);
  176.  
  177.     /* Now, register our native methods. */
  178.     if (initResult == noErr)
  179.     {
  180.         /* Create a new Java runtime environment. */
  181.         runtimeEnv = JRI_NewEnv(runtimeInstance, NULL);
  182.  
  183.         if (runtimeEnv != NULL)
  184.         {
  185.             /* Get a cookie for the class whose methods we implement. */
  186.             classID = JRI_FindClass(runtimeEnv, kJRIClassName);
  187.  
  188.             if (classID == nil)
  189.             {
  190.                 initResult = fnfErr;
  191.             }
  192.             else
  193.             {
  194.                 /* Register the methods. */
  195.                 JRI_RegisterNatives
  196.                 (
  197.                     runtimeEnv,
  198.                     classID,
  199.                     methodNames,
  200.                     methodProcedures
  201.                 );
  202.             }
  203.  
  204.             /* Clean up the environment we used. */
  205.             JRI_DisposeEnv(runtimeInstance, runtimeEnv);
  206.         }
  207.     }
  208.     
  209.     return initResult;
  210. }
  211.